package com.central.apps.controller;

import com.central.app.common.auth.DataAuthority;
import com.central.app.service.HandlerService;
import com.central.apps.util.HttpUtil;
import com.central.common.util.ToolUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author mengweitao
 * @Date 2022/11/30 12:10
 * 库存盘点子表数据导出
 */
@Log4j2
@RestController
@RequestMapping("/PaymentApplyExport")
public class PaymentApplyExportController {

    @Autowired
    private HandlerService handlerService;

    @Autowired
    private DataAuthority dataAuthority;

    /**
     * 库存盘点导出
     * @param response
     * @throws IOException
     */
    @PostMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response,@RequestParam String ids) throws IOException {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("Sheet1");
        HSSFRow row = sheet.createRow(0);

        row.createCell(0).setCellValue("申请单号");
        row.createCell(1).setCellValue("申请描述");
        row.createCell(2).setCellValue("付款类型");
        row.createCell(3).setCellValue("付款金额");
        row.createCell(4).setCellValue("状态");
        row.createCell(5).setCellValue("创建人");
        row.createCell(6).setCellValue("创建日期");
//        //这三个值是子表数据，需要查询子表进行赋值
//        row.createCell(7).setCellValue("发票号");
//        row.createCell(8).setCellValue("物料编码");
//        row.createCell(9).setCellValue("电商订单号");

        int rownum=1;
        List<Map<String, Object>> bySql = new ArrayList<>();

        if(StringUtils.isNotBlank(ids)){
            bySql = handlerService.findBySql("select * from gq_paymentapply where id in (" + ids + ")");
        }else{
            String wheres = " AND (t.`concode` IS NULL OR t.`concode` = '') ";
            try {
               String  where = dataAuthority.findDsfDept2("paymentapplyApp",null,null);
               if(ToolUtil.isNotEmpty(where)){
                   wheres=wheres+where;
               }
            } catch (Exception e) {
                e.printStackTrace();
            }


            bySql = handlerService.findBySql("select * from gq_paymentapply t where 1=1 "+wheres+"  order by createtime desc");
        }
        if(CollectionUtils.isNotEmpty(bySql)){
            for (Map<String, Object> map : bySql) {
                HSSFRow row1 = sheet.createRow(rownum);
                row1.createCell(0).setCellValue(MapUtils.getString(map,"payapplynum"));
                row1.createCell(1).setCellValue(MapUtils.getString(map,"payapplydesc"));
                row1.createCell(2).setCellValue(MapUtils.getString(map,"paymenttype"));
                row1.createCell(3).setCellValue(MapUtils.getString(map,"receivingamounttax"));
                row1.createCell(4).setCellValue(MapUtils.getString(map,"status"));
                row1.createCell(5).setCellValue(MapUtils.getString(map,"createuserdesc"));
                row1.createCell(6).setCellValue(MapUtils.getString(map,"createtime"));
//                //查询子表数据
//                List<Map<String,Object>> list = handlerService.findBySql("select pinum,itemnum,ecnum from gq_paypursumary where puid = "+map.get("id"));
//                StringBuffer pinum = new StringBuffer();
//                StringBuffer itemnum = new StringBuffer();
//                StringBuffer ecnum = new StringBuffer();
//                if(CollectionUtils.isNotEmpty(list)){
//                    for (Map<String, Object> stringObjectMap : list) {
//                        if(ToolUtil.isNotEmpty(stringObjectMap.get("pinum"))){
//                            pinum.append(",").append(stringObjectMap.get("pinum"));
//                        }
//                        if(ToolUtil.isNotEmpty(stringObjectMap.get("itemnum"))){
//                            itemnum.append(",").append(stringObjectMap.get("itemnum"));
//                        }
//                        if(ToolUtil.isNotEmpty(stringObjectMap.get("ecnum"))){
//                            ecnum.append(",").append(stringObjectMap.get("ecnum"));
//                        }
//
//                    }
//                    row1.createCell(7).setCellValue(pinum.toString().replaceFirst(",",""));
//                    row1.createCell(8).setCellValue(itemnum.toString().replaceFirst(",",""));
//                    row1.createCell(9).setCellValue(ecnum.toString().replaceFirst(",",""));
//                }
                rownum++;
            }
        }
        try {
            HSSFSheet sheet2 = wb.createSheet("Sheet2");
            setItemInfo(sheet2,ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //输出Excel文件
        OutputStream output = response.getOutputStream();
        response.reset();
        String exportFileName ="付款申请导出.xls";
        String fileNameURL = URLEncoder.encode(exportFileName, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename="+fileNameURL+";"+"filename*=utf-8''"+fileNameURL);
        response.setContentType("application/x-xls");
        wb.write(output);
        output.close();

    }

    private void setItemInfo(HSSFSheet sheet ,String ids){
        HSSFRow row = sheet.createRow(0);

        row.createCell(0).setCellValue("发票编号");
        row.createCell(1).setCellValue("物料编码");
        row.createCell(2).setCellValue("物料名称");
        row.createCell(3).setCellValue("型号");
        row.createCell(4).setCellValue("单位名称");
        row.createCell(5).setCellValue("发票金额");
        row.createCell(6).setCellValue("未付金额");
        //这三个值是子表数据，需要查询子表进行赋值
        row.createCell(7).setCellValue("申请金额");
        row.createCell(8).setCellValue("付款金额");

        int rownum=1;
        List<Map<String, Object>> bySql = new ArrayList<>();

        if(StringUtils.isNotBlank(ids)){
            bySql = handlerService.findBySql("select * from gq_paymentapply where id in (" + ids + ")");
        }else{
            String wheres = " AND (t.`concode` IS NULL OR t.`concode` = '') ";
            try {
                String  where = dataAuthority.findDsfDept2("paymentapplyApp",null,null);
                if(ToolUtil.isNotEmpty(where)){
                    wheres=wheres+where;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }


            bySql = handlerService.findBySql("select * from gq_paymentapply t where 1=1 "+wheres+"  order by createtime desc");
        }
        if(CollectionUtils.isNotEmpty(bySql)){
            for (Map<String, Object> map : bySql) {
                HSSFRow row1 = sheet.createRow(rownum);

                //查询子表数据
                List<Map<String,Object>> list = handlerService.findBySql("select * from gq_paypursumary where puid = "+map.get("id"));
                if(CollectionUtils.isNotEmpty(list)){
                    for (Map<String, Object> stringObjectMap : list) {
                        row1.createCell(0).setCellValue(MapUtils.getString(stringObjectMap,"pinum"));
                        row1.createCell(1).setCellValue(MapUtils.getString(stringObjectMap,"itemnum"));
                        row1.createCell(2).setCellValue(MapUtils.getString(stringObjectMap,"itemdesc"));
                        row1.createCell(3).setCellValue(MapUtils.getString(stringObjectMap,"spemodel"));
                        row1.createCell(4).setCellValue(MapUtils.getString(stringObjectMap,"suppliername"));
                        row1.createCell(5).setCellValue(MapUtils.getString(stringObjectMap,"pricetaxtotal"));
                        row1.createCell(6).setCellValue(MapUtils.getString(stringObjectMap,"faplymoney"));
                        row1.createCell(7).setCellValue(MapUtils.getString(stringObjectMap,"applymoney"));
                        row1.createCell(8).setCellValue(MapUtils.getString(stringObjectMap,"paidamt"));

                    }

                }
                rownum++;
            }
        }

    }
}
